<?php

namespace App\Http\Controllers\Api\Backend;

use App\Models\User;
use App\Utils\FundType;
use App\Models\Recharge;
use App\Models\UserFund;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class RechargeController extends Controller
{
    public function index(Request $request)
    {
        $userId = 0;
        if (isset($request->username)) {
            $userId = User::where('username', $request->username)->first()['id'];
        }
        $limit = $request->limit ?? 10;
        $orderNo = $request->order_no;
        $verify_status = $request->verify_status ?? -2;
        $startDate = $request->startDate;
        $endDate = $request->endDate ? $request->endDate . ' 23:59:59' : null;;
        $orderBy = $request->orderBy ? 'asc' : 'desc';
        $recharges = Recharge::when($orderNo, function ($query) use ($orderNo) {
            return $query->where('order_no', 'like', '%' . $orderNo . '%');
        })->when($verify_status > -2, function ($query) use ($verify_status) {
            return $query->where('verify_status', $verify_status);
        })->when($userId > 0 || isset($request->username), function ($query) use ($userId) {
            return $query->where('user_id', $userId);
        })->when($startDate, function ($query) use ($startDate) {
            return $query->where('created_at', '>=', $startDate);
        })->when($endDate, function ($query) use ($endDate) {
            return $query->where('created_at', '<=', $endDate);
        })->with(['user:id,username,nickname'])->orderBy('id', $orderBy)->paginate($limit);

        return success($recharges);
    }

    // public function store(Request $request)
    // {
    //     $data = $request->all();
    //     $data['order_no'] = getOrderNo();
    //     $recharge = Recharge::create($data);
    //     if (isset($recharge)) {
    //         return success();
    //     }
    //     return error();
    // }

    public function show($id)
    {
        $recharge = Recharge::find($id);
        return success($recharge);
    }

    // public function update(Request $request, $id)
    // {
    //     $data = $request->all();
    //     if (Recharge::where('id', $id)->update($data)) {
    //         return success();
    //     }
    //     return error();
    // }

    // public function destroy($id)
    // {
    //     if (Recharge::where('id', $id)->delete()) {
    //         return success();
    //     }
    //     return error();
    // }

    public function verify(Request $request, $id)
    {
        $verify_status = $request->verify_status;
        $remark = $request->remark ?? null;
        DB::beginTransaction();  //开启事务
        try {
            $recharge = Recharge::find($id);
            if ($recharge->verify_status != 0) {
                return errorMsg('重复审核');
            }
            $user = User::find($recharge->user_id);
            $priceTotal = $request->real_price;
            if ($verify_status == -1) {
                UserFund::create([
                    'user_id' => $user->id,
                    'type' => FundType::RECHARGE,
                    'change_amount' => 0,
                    'after_amount' => $user->amount,
                    'content' =>  '驳回原因:' . $remark,
                    'remark' => '充值驳回'
                ]);
            } else {
                $recharge->real_price = $priceTotal;
                $user->increment('amount', $priceTotal);
                UserFund::create([
                    'user_id' => $user->id,
                    'type' => FundType::RECHARGE,
                    'change_amount' => $priceTotal,
                    'after_amount' => $user->amount,
                    'content' =>  '充值成功，充值余额:' . $recharge->price . '元,实际到账' . $priceTotal . '元',
                    'remark' => '充值成功'
                ]);
            }
            $recharge->verify_status = $verify_status;
            $recharge->remark = $remark;
            $recharge->save();
            DB::commit();
            return success();
        } catch (\Exception $e) {
            DB::rollback();
            return error($e);
        }
    }
}
